package cn.schoolwow.quickdao.test.module.business.user.ddl.testcase;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.domain.entity.common.TableColumn;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickdao.test.module.business.user.dml.user.flow.add.RebuildUserFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

public class CreatePositionTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        flowContext.executeFlowList(new RebuildUserFlow());
        createPositionColumn(flowContext);
        checkResult(flowContext);
    }

    @Override
    public String name() {
        return "用户表添加岗位字段用例";
    }

    private void createPositionColumn(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);

        Entity entity = dao.getEntity(User.class);

        TableColumn tableColumn = new TableColumn();
        tableColumn.column = "position";
        tableColumn.columnType = "varchar(32)";
        tableColumn.comment = "岗位";
        dao.createColumn(entity.tableName, tableColumn);
        flowContext.putCurrentFlowInstanceData(tableColumn);
    }

    private void checkResult(FlowContext flowContext){
        TableColumn tableColumn = flowContext.checkInstanceData(TableColumn.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        Property positionProperty = dao.getProperty(User.class, tableColumn.column);
        Assert.assertEquals(positionProperty.column, tableColumn.column);
        Assert.assertTrue(tableColumn.columnType.contains(positionProperty.columnType));
    }
}
